import stripe

stripe.api_key = "sk_test_51Ogsw5B0IyqaSJBrwczlr820jnmvA1qQQGoLZ2XxOsIzikpmXo4pRLjw4XVMTEBR8DdVTYySiAv1XX53Zv5xqynF00GfMqttFd"


class ProductService:
    def __init__(self):
        pass

    def create_product(
            self,
            name: str,
            description: str = None,
            default_price_data: dict = None,
    ) -> str:
        """
        :param name:
        :param description:
        :param default_price_data:
        :return: product_id
        """
        product = stripe.Product.create(
            name=name,
            description=description,
            default_price_data=default_price_data
        )
        return product.id

    def create_price_for_product(self,
                                 product_id: str,
                                 unit_amount: int,
                                 currency: str = "usd",
                                 interval: str = "year") -> str:
        """
        :param product_id:
        :param unit_amount:
        :param currency:
        :param interval:
        :return: price_id
        """
        # 也可单独创建价格（关联已有产品）
        price = stripe.Price.create(
            product=product_id,  # 关联上述产品
            unit_amount=unit_amount,  # 分
            currency=currency,
            recurring={"interval": interval},  # 年度计费（$19.99/年）
        )
        return price.id


if __name__ == '__main__':
    service = ProductService()
    product_id = service.create_product("TTFreeleaps")

    price_id1 = service.create_price_for_product(product_id, 10)
    price_id2 = service.create_price_for_product(product_id, 20)
    print("产品Id", product_id, "会员价格", price_id1, "Pro会员价格", price_id2)
